﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Noebe;
using Pixysoft.SQL;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Framework.Reflection;
using Pixysoft.Tools;

namespace Pixysoft.Weblications.Sango.Dao
{
    class UsrResourceDao
    {
        public static List<Entities.Usr_Resource> GetUserResources(string usercode)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = SqlCreator.SelectAll().From(Schemas.LOCAL.USR_RESOURCE)
            .Where(Schemas.LOCAL.USR_RESOURCE.USERCODE).Equal("USERCODE", true)
            .And(Schemas.LOCAL.USR_RESOURCE.STATUS).Equal("STATUS", true).GetSQL();

            command.Parameters.Add("USERCODE", usercode);
            command.Parameters.Add("STATUS", GeneralStatusCollection.OPEN);
            return command.ExecuteReader<Entities.Usr_Resource>();
        }

        internal static List<Pixysoft.Weblications.Sango.Entities.Usr_Resource> GetResources(
    long sectionserialcode, long scaletypecode)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = SqlCreator.SelectAll().From(Schemas.LOCAL.USR_RESOURCE)
            .Where(Schemas.LOCAL.USR_RESOURCE.SECTIONSERIALCODE).Equal("SECTIONSERIALCODE", true)
            .And(Schemas.LOCAL.USR_RESOURCE.SECTIONSCALETYPECODE).Equal("SCALETYPECODE", true)
            .And(Schemas.LOCAL.USR_RESOURCE.STATUS).Equal("STATUS", true)
            .GetSQL();

            command.Parameters.Add("SECTIONSERIALCODE", sectionserialcode);
            command.Parameters.Add("SCALETYPECODE", scaletypecode);
            command.Parameters.Add("STATUS", GeneralStatusCollection.OPEN);
            return command.ExecuteReader<Entities.Usr_Resource>();
        }

        internal static Entities.Usr_Resource TryGetResource(string primarycode, string userCode)
        {
            Entities.Usr_Resource orm = SettingHelper.GetNoebe().EntityNoebeCommand.SelectByPk<Entities.Usr_Resource>(primarycode);

            if (orm == null)
                return null;

            if (orm.Status != GeneralStatusCollection.OPEN)
                return null;

            if (!orm.Usercode.Equals(userCode, StringComparison.OrdinalIgnoreCase))
                return null;

            return orm;
        }

        internal static Pixysoft.Weblications.Sango.Entities.Usr_Resource TryGetOccupyResource(
            long sectionserialcode, long positionindex)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = SqlCreator.SelectAll().From(Schemas.LOCAL.USR_RESOURCE)
            .Where(Schemas.LOCAL.USR_RESOURCE.SECTIONSERIALCODE).Equal("SECTIONSERIALCODE", true)
            .And(Schemas.LOCAL.USR_RESOURCE.POSITIONINDEX).Equal("POSITIONINDEX", true)
            .And(Schemas.LOCAL.USR_RESOURCE.STATUS).Equal("STATUS", true)
            .GetSQL();

            command.Parameters.Add("SECTIONSERIALCODE", sectionserialcode);
            command.Parameters.Add("POSITIONINDEX", positionindex);
            command.Parameters.Add("STATUS", GeneralStatusCollection.OPEN);

            List<Entities.Usr_Resource> list = command.ExecuteReader<Entities.Usr_Resource>();
            if (list.Count == 0)
                return null;

            return list[0];
        }

        internal static Entities.Usr_Resource createResource(
            Entities.Usr_Profile profile, Entities.Usr_Section section, Entities.Cti_Resource cityResource)
        {
            Entities.Usr_Resource orm = ReflectionManager.CreatePojo<Entities.Usr_Resource>();
            orm.Primarycode = PkHelper.PrimaryCode();
            orm.Usercode = profile.Usercode;
            orm.Nickname = profile.Nickname;
            orm.Sectionserialcode = section.Sectionserialcode;
            orm.Resourceserialcode = cityResource.Resourceserialcode;
            orm.Positionindex = cityResource.Positionindex;
            orm.Startdate = GlobalTimer.Instance.ToTimeSequence();
            orm.Enddate = ResourceFormular.GetResourceTimespan();
            orm.Sectionscaletypecode = section.Scaletypecode;
            orm.Status = GeneralStatusCollection.OPEN;
            return orm;
        }
    }
}
